package com.rongke.web.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.JsonResp;
import com.rongke.mapper.UserCollectShareMapper;
import com.rongke.model.User;
import com.rongke.model.UserCollectShare;
import com.rongke.service.UserCollectShareService;

import com.rongke.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

/**
 * @UserCollectShareController
 * @用户收藏股票Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/userCollectShare")
@Transactional
@CrossOrigin
public class UserCollectShareController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private UserCollectShareService userCollectShareService;
    @Autowired
    private UserService userService;
    /**
     * @添加用户收藏股票
     * @param userCollectShare
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public JsonResp addUserCollectShare(@RequestBody UserCollectShare userCollectShare){
        log.debug("添加用户收藏股票");
        userCollectShareService.insert(userCollectShare);
        return JsonResp.ok(userCollectShare);
    }

    /**
     * @修改用户收藏股票
     * @param userCollectShare
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateUserCollectShare(@RequestBody UserCollectShare userCollectShare){
        log.debug("修改用户收藏股票");
        userCollectShareService.updateById(userCollectShare);
        return JsonResp.ok(userCollectShare);
    }
    /**
     * @根据id查找用户收藏股票
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectUserCollectShare(Long id){
        log.debug("查找用户收藏股票");
        UserCollectShare userCollectShare = userCollectShareService.selectById(id);
        return JsonResp.ok(userCollectShare);
    }

    /**
     * @查找用户收藏股票
     * @param
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectCollectionStock", method = RequestMethod.GET)
    public JsonResp selectCollectionStock(){
        log.debug("查找用户收藏股票");
        User loginUser=userService.findLoginUser();
        EntityWrapper<UserCollectShare> ew=new EntityWrapper<>();
        ew.eq("user_id",loginUser.getId());
        List<UserCollectShare> list = userCollectShareService.selectList(ew);
        return JsonResp.ok(list);
    }

    /**
     * @删除用户收藏股票
     * @param
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/deleteCollectionStock", method = RequestMethod.GET)
    public JsonResp deleteCollectionStock(String code){
        log.debug("删除用户收藏股票");
        User loginUser=userService.findLoginUser();
        EntityWrapper<UserCollectShare> ew=new EntityWrapper<>();
        ew.eq("user_id",loginUser.getId());
        ew.eq("share_code",code);
        userCollectShareService.delete(ew);
        return JsonResp.ok();
    }

    /**
     * @添加自选股票
     * @param
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/addCollectionStock", method = RequestMethod.GET)
    public JsonResp addCollectionStock(String code){
        log.debug("添加自选股票");
        User loginUser=userService.findLoginUser();
        EntityWrapper<UserCollectShare> ew=new EntityWrapper<>();
        ew.eq("user_id",loginUser.getId());
        ew.eq("share_code",code);
        UserCollectShare ucs=userCollectShareService.selectOne(ew);
        if(ucs !=null){
            return JsonResp.fa("您已添加过，不能重复添加");
        }
        UserCollectShare userCollectShare=new UserCollectShare();
        userCollectShare.setUserId(loginUser.getId());
        userCollectShare.setShareCode(code);
        userCollectShareService.insert(userCollectShare);
        return JsonResp.ok();
    }


}
